package com.situ.edu.dao.impl;

import com.situ.edu.dao.IStudentCouDao;
import com.situ.edu.pojo.entity.Elective;
import com.situ.edu.pojo.entity.Student;
import com.situ.edu.pojo.query.CourseQuery;
import com.situ.edu.pojo.vo.CourseVO;
import com.situ.edu.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

public class StudentCouDaoImpl implements IStudentCouDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public List<CourseVO> selectByPage(CourseQuery courseQuery) {
        String sql = "SELECT c.id,c.`name`,c.gredit,c.xi_id,t.place,x.`name` AS xiName \n" +
                "FROM course AS c INNER JOIN taught AS t\n" +
                "ON c.id = t.course_id INNER JOIN xi AS x\n" +
                "ON x.id=c.xi_id where 1=1";

        List<Object> list = new ArrayList<>();
        if (!ObjectUtils.isEmpty(courseQuery.getXiId())){
            sql += " and c.xi_id=?";
            list.add(courseQuery.getXiId());
        }
        sql += " limit ?,?";
        int offset = (courseQuery.getPage() - 1)*courseQuery.getLimit();
        list.add(offset);
        list.add(courseQuery.getLimit());

        return template.query(sql,
                new BeanPropertyRowMapper<>(CourseVO.class),list.toArray());
    }

    @Override
    public int selectTotalCount(CourseQuery courseQuery) {
        String sql = "SELECT count(*) \n" +
                "FROM course AS c INNER JOIN taught AS t\n" +
                "ON c.id = t.course_id INNER JOIN xi AS x\n" +
                "ON x.id=c.xi_id where 1=1";

        List<Object> list = new ArrayList<>();
        if (!ObjectUtils.isEmpty(courseQuery.getXiId())){
            sql += " and c.xi_id=?";
            list.add(courseQuery.getXiId());
        }
        return template.queryForObject(sql,Integer.class,list.toArray());
    }

    @Override
    public void add(Elective elective) {
        template.update("insert into elective(student_id,course_id) values (?,?)",
                elective.getStudentId(),elective.getCourseId());
    }



    @Override
    public List<Student> selectAll() {
        return template.query("select * from student",new BeanPropertyRowMapper<>(Student.class));
    }

    @Override
    public void update(Elective elective) {
        template.update("insert into elective(student_id,course_id) values (?,?)",
                elective.getStudentId(),elective.getCourseId());
    }


    @Override
    public Student selectById(int id) {

        return template.queryForObject("select * from student where user_id=?",
                new BeanPropertyRowMapper<>(Student.class),id);
    }

    @Override
    public void addElective(int id, int courseId) {
        template.update("insert into elective(student_id,course_id) values (?,?)",id,courseId);
    }

    /*@Override
    public Student selectById(String name) {
        return template.queryForObject("SELECT * from student WHERE user_id=(SELECT id from `user` WHERE name=?)",
                new BeanPropertyRowMapper<>(Student.class),name);
    }*/

    @Override
    public void updateStu(Student student) {
        template.update("update student set name=?,age=?,gender=? where id=?",
                student.getName(),student.getAge(),student.getGender(),student.getId());
    }
}
